//  -------------------- iSharePoint -------------------- //
// 
//  Company:	IT Complex, LLC
//  Project:	Smart.Common
//  File name:	EntityInfo.cs
//  Developer:	Solomatov Igor
//  Created:	15.04.2012
// 
//  -------------------- iSharePoint -------------------- //

using System;
using System.Diagnostics;

namespace Smart.Common.Entities
{
	/// <summary>
	/// Entity information
	/// </summary>
	[DebuggerDisplay("EntityInfo {Name}: {Type}")]
	public class EntityInfo
	{
		public string Name { get; set; }
		public string ContentType { get; set; }
		public Type Type { get; set; }

		public EntityInfo(Type type)
		{
			Type = type;

			var akea = Type.GetCustomAttributes(typeof(EntityAttribute), false);

			if (akea == null || akea.Length <= 0 || akea.Length > 1)
				throw new NotSupportedException();

			var kea = (EntityAttribute)akea[0];

			Name = kea.EntityName;

			var acta = Type.GetCustomAttributes(typeof(ContentTypeAttribute), false);

			if (acta != null && acta.Length == 1)
			{
				var cta = (ContentTypeAttribute)acta[0];

				ContentType = cta.ContentTypeName;
			}
		}

		public override bool Equals(object obj)
		{
			if (obj == null || GetType() != obj.GetType())
			{
				return false;
			}

			return Name.EqIC(((EntityInfo)obj).Name);
		}

		public override int GetHashCode()
		{
			return StringComparer.OrdinalIgnoreCase.GetHashCode(Name);
		}
	}
}